package com.jse.template.thymeleaf;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.WebContext;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;

import com.jse.Jse;

public class ThymeleafFactory {
	
	TemplateEngine templateEngine;
	
	public ThymeleafFactory() {
		ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver(Jse.getSc());
		// XHTML is the default mode, but we will set it anyway for better understanding of code
        templateResolver.setTemplateMode(TemplateMode.HTML);
        
    // This will convert "home" to "/WEB-INF/templates/home.html"
    // 设置模板的前置路径
        templateResolver.setPrefix("/");
    //设置模板统一的后缀名
        templateResolver.setSuffix(".html");
        // Set template cache TTL to 1 hour. If not set, entries would live in cache until expelled by LRU
        templateResolver.setCacheTTLMs(Long.valueOf(3600000L));
        
        // Cache is set to true by default. Set to false if you want templates to
        // be automatically updated when modified.
        templateResolver.setCacheable(true);
        
        templateEngine = new TemplateEngine();
        templateEngine.setTemplateResolver(templateResolver);
	}
	
	public void xx(String template,HttpServletRequest req,HttpServletResponse resp) throws IOException {
		WebContext ctx = new WebContext(req, resp, req.getServletContext(), req.getLocale());
		templateEngine.process(template, ctx, resp.getWriter());
	} 
}
